<template>
	<view>
		<block>
			<uni-nav-bar @clickLeft="goBack" status-bar="true" shadow="true" left-icon="back" left-text="返回" title="登录"></uni-nav-bar>
		</block>
		<view class="uni-form-item " >
			<view class="title">手机号</view>
			<input v-model="temphone" @focus="getFocus(0)" class="uni-input " v-bind:class="{ 'input-error': hasError[0] }"
			 placeholder="请输入手机号" />
		</view>
		<view class="uni-form-item " v-if="!isActivateSession">
			<view class="title">密码</view>
			<input v-model="password" @focus="getFocus(1)" v-bind:class="{ 'input-error': hasError[1] }" class="uni-input"
			 placeholder="请输入密码" />
		</view>
		
		<button v-else class="button" type="primary" @click="weixinAutoLogin">微信一键登录</button>
		<view style="  width: 100%; text-align: center; padding-top: 10px;" >
			<text style="color: #007AFF; font-size:medium;"  @click="goTelephoneLogin">其他号码登录/注册</text>
		</view>
		</view>
		
		<!-- 通常应该申请手机号权限，然后进行一键登录操作。（目前可能只针对认证用户开放） -->
		<!-- <button v-else  class="authority-button" @getPhoneNumber="getPhoneNumber" open-type="getPhoneNumber" type="default">微信一键登录</button> -->
	</view>
</template>

<script>
	import uniNavBar from "@/components/uni-nav-bar/uni-nav-bar.vue"
	import config from "@/common/config.js"
	export default {
		components: {
			uniNavBar
		},
		data() {
			return {
				temphone: '',
				password: '',
				hasError: [false, false],
				isActivateSession: false,
				sessionkey: ''
			}
		},
		onLoad(option) {
			if(option.from=='my'){//直接账号密码登录
				console.log("直接账号密码登录==");
				return;
			}
			//检查weixin的session是否过期
			uni.checkSession({
				success: (e) => {
					this.sessionkey = uni.getStorageSync(config.sessionkey);
					if (this.sessionkey.length != 0) {
						this.isActivateSession = true; //为了测试暂定为false
						console.log("wei_guo_qi ==" + this.sessionkey);
					}else{
						this.isActivateSession = false;
						console.log("yi_guo_qi_ session")
						
					}
				},
				fail: () => {
					this.isActivateSession = false;
					console.log("yi_guo_qi_ session");
					
				}
			})
		},
		methods: {
			//通常应该申请手机号权限，然后进行一键登录操作。（目前可能只针对认证用户开放）
			// getPhoneNumber(e) {
			// 	var phoneNumber = e.detail.phoneNumber;
			// 	if (phoneNumber) {
			// 		console.log('申请使用手机号成功');
			// 		//授权成功后第一时间去绑定账号
			// 		// this.toLoginWeixinAndGetSessionkey(userInfo);
			// 	} else {
			// 		console.log('申请使用手机号被拒');
			// 	}
			// },
			//一键自动登录
			weixinAutoLogin() {
				var url = config.baseUrl + '/thirdbind/autologinbyweixinsessionkey';
				var data = {
					sessionkey: this.sessionkey,
					name:this.temphone //后端 name就是手机号
				};
				uni.request({
					method: 'POST',
					url: url,
					data: data,
					header: {
						'content-type': 'application/json',
					},
					success: (res) => {
						// console.log(res);
						var statusCode = res.statusCode;
						console.log(statusCode);
						if (200 == statusCode) {
							//保存token
							console.log("一键登录成功pppppp");
							console.log(res.data);
							uni.setStorageSync(config.token, res.data.token);
							uni.setStorageSync('telephone', res.data.thirdBindNew.telephone);
							uni.setStorageSync('nickname', res.data.thirdBindNew.nickname);
							uni.setStorageSync('avatarurl', res.data.thirdBindNew.avatarurl);
						
							uni.showToast({
								title: "一键登录成功",
								duration: 2000,
							});
							// uni.navigateBack();
							uni.switchTab({
							    url: '../wode/wode'
							});
						} else {
							uni.showToast({
								title: "一键登录失败else",
								duration: 2000,
							});
						}
					},
					fail: (res) => {
						uni.showToast({
							title: "一键登录失败fail"
						});
					}
				});
			},
			goTelephoneLogin(){
				uni.navigateTo({
					url:'../loginbytelephone/loginbytelephone'
				})
			},
			// //账号密码登录
			// goLogin() {
			// 	if (this.temphone.length == 0) {
			// 		this.hasError.splice(0, 1, true);
			// 		return;
			// 	}
			// 	if (this.password.length == 0) {
			// 		this.hasError.splice(1, 1, true);
			// 		return;
			// 	}
			// 	var url = config.baseUrl + '/users/login';
			// 	var name = this.temphone;
			// 	var password = this.password;
			// 	var data = {
			// 		name,
			// 		password
			// 	};
			// 	uni.request({
			// 		method: 'POST',
			// 		url: url,
			// 		data: data,
			// 		header: {
			// 			'content-type': 'application/json',
			// 		},
			// 		success: (res) => {
			// 			// console.log(res);
			// 			var statusCode = res.statusCode;
			// 			console.log(statusCode);
			// 			if (200 == statusCode) {
			// 				//保存token
			// 				console.log(res.data.token);
			// 				uni.setStorageSync(config.token, res.data.token);
			// 				//用其他账号登录后，便于重新绑定
			// 				var localTelephone = uni.getStorageSync(config.telephoneKey);
			// 				if(localTelephone){
			// 					if(!(localTelephone==this.temphone)){
			// 						console.log('logcla::'+uni.getStorageSync(config.telephoneKey));
			// 						console.log('logclattt::'+config.telephoneKey)
			// 						uni.removeStorageSync(config.sessionkey);
			// 						uni.setStorageSync(config.telephoneKey,this.temphone);
			// 					}
			// 				}else{
			// 					uni.setStorageSync(config.telephoneKey,this.temphone);
			// 				}
			// 				uni.showToast({
			// 					title: "登录成功",
			// 					duration: 2000,
			// 				});
			// 				uni.navigateBack();
			// 			} else {
			// 				uni.showToast({
			// 					title: "else:"+url,
			// 					duration: 5000,
			// 				});
			// 			}
			// 		},
			// 		fail: (res) => {
			// 			uni.showToast({
			// 				title: "登录失败f"
			// 			});
			// 		}
			// 	});
				
			// },
			getFocus(indxe) {
				this.hasError.splice(indxe, 1, false);
			},
			goBack() {
				uni.navigateBack({

				})
			}
		}
	}
</script>

<style>
	.input-error {
		border: 2upx solid #FF3333;
	}
	.title{
		font-size: medium;
	}
	.uni-input{
		font-size: medium;
	}
</style>
